import { _decorator, Vec3 , Node, random, randomRangeInt} from 'cc';
import { SkillManager } from './SkillManager';
import { EBuff, EGroup, EntityType, ESkill } from '../Config/Enum';
import ObjectPoolManager from '../Core/ObjectPoolManager';
import { Collider2D } from 'cc';
import { PlayerManager } from '../PlayerManager';
import { buffConfig, SkillConfig } from '../Config/SkillConfig';
import { EnemyManager } from '../EnemyManager';
import { TangmenExplosion } from './TangmenExplosion';

const { ccclass } = _decorator;
//武学3：陷阱绝技，随机扔出陷阱（没有触发会持续存在）每秒150%伤害
// 符文1 - 七绝杀阵 陷阱数量+3
// 符文2 - 困兽犹斗 陷阱范围扩大50%，控制+1秒
// 符文3 - 毒爆送葬 陷阱造成超远的击退


@ccclass('TangmenSkill3')
export class TangmenSkill3 extends SkillManager {
    private _hasSplit = false;
    runeId
    effectChance
    buffTypeList
    buffDuration
    numOfBullets = 3; // 默认是3个陷阱

    

    // 新增标志：是否是主子弹（由玩家直接发射的子弹）
    private isMasterBullet = true;


    init(type: ESkill, pm: PlayerManager) {

        // 重置所有状态

        //this.reset();
        super.init(type, pm);

        this.node.angle = 0


        if (this.level <= 6 && this.level > 3) {
            this.numOfBullets = 4
        } else if (this.level <= 10 && this.level > 6) {
            this.numOfBullets = 5
        }
        

        // 其他符文效果
        switch (this.runeId) {
            case 1:
                this.numOfBullets += 5
                break;
            case 2:
                break;  
            case 3:
                break;
            default:
                break;
        }



        // 只有主子弹需要重置分裂标志
        if (this.isMasterBullet) {
            this._hasSplit = false;
        }


        // 分裂成多个陷阱
        if (this.isMasterBullet && !this._hasSplit) {

            this.splitIntoMultipleBullets(this.numOfBullets); // 释放多个陷阱
            this._hasSplit = true; // 标记为已分裂
            ObjectPoolManager.Instance.ret(this.node); // 立即回收主子弹
            
        }

    }


    private splitIntoMultipleBullets(count: number) {
        console.log("tangmeng3 numofbullets:", this.numOfBullets)


        // 创建多个子弹
        for (let i = 0; i < count; i++) {
            let postion = new Vec3(randomRangeInt(-180, 180),randomRangeInt(-300, 300), 0); 

            const bulletNode = ObjectPoolManager.Instance.get(ESkill.TangmenSkill3) as Node;
            if (!bulletNode) {
                console.error("Failed to get TangmenSkill3 from pool");
                return;
            }


            const bullet = bulletNode.getComponent(TangmenSkill3);
            if (!bullet) {
                console.error("TangmenSkill3 component not found on node");
                return;
            }

            // 初始化子弹

            bullet.isMasterBullet = false; // 标记为子子弹
            bullet._hasSplit = true; // 子子弹不需要再次分裂

            bullet.init(this.type, this.pm);
            bulletNode.setPosition(postion);

            // // 设置位置和方向
            // bullet.node.setPosition(this.node.position);
            // bullet.direction = this.direction.clone(); // 继承主子弹的方向

            // // 将子弹添加到场景中
            // this.pm.addBullet(bulletNode);
        }

        // 原始子弹完成任务，立即回收
        ObjectPoolManager.Instance.ret(this.node);
    }

    protected onHitEnemy(self, other: Collider2D): void {
        console.log("TangmenSkill3 init pm30", this.pm);



        // 触碰陷阱触发爆炸
        let explosionNode = ObjectPoolManager.Instance.get(ESkill.TangmenExplosion) as Node;
        explosionNode.setPosition(other.node.position);

        // 传递必要参数给爆炸实体
        const explosionSkill = explosionNode.getComponent(TangmenExplosion);
        if (explosionSkill) {
            explosionSkill.level = this.level;
            explosionSkill.runeId = this.runeId;
            explosionSkill.damage = this.damage; // 传递基础伤害
            explosionSkill.playSpeed = 0.1; // 标记为子子弹
            explosionSkill.criticalChance = 0; // 标记为子子弹
            explosionSkill.scale = 5; // 标记为子子弹
            explosionSkill.init(ESkill.TangmenExplosion, this.pm);

            //explosionSkill.pm = this.pm; // 标记为子子弹
            //explosionSkill.duration = false; // 标记为子子弹

        }

        // 回收陷阱本体（一次性陷阱）
        ObjectPoolManager.Instance.ret(this.node);


    }


// reset() {
//     this._hasSplit = false;
//     this.isMasterBullet = true;
//     this.direction = new Vec3(0, 0, 0);
//     this._contactPosition = new Vec3(0, 0, 0);
// }





}